package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/address")
public class AddressController {
    @Reference
    private AddressService addressService;
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Address> addressList = addressService.findAll();
        if(addressList!=null && addressList.size()>0){
            Result result = new Result(true,MessageConstant.GET_ADDRESS_SUCCESS,addressList);
            result.setData(addressList);
            return result;
        }
        return new Result(false,MessageConstant.GET_ADDRESS_FAIL);
    }
    @RequestMapping("/findByName")
    public Result findByName(String addressName){
        try {
            Address address = addressService.findByName(addressName);
            Result result = new Result(true,MessageConstant.GET_ADDRESS_SUCCESS,address);
            result.setData(address);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ADDRESS_FAIL);
        }

    }
}
